package edu.tyut.game.service.impl;

import edu.tyut.game.model.User;
import edu.tyut.game.service.EmailService;
import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

@Slf4j
@Service
public class EmailServiceImpl implements EmailService {

    @Value("${spring.mail.username}")
    private String from;

    private JavaMailSender javaMailSender;

    @Autowired
    public void setJavaMailSender(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    private TemplateEngine templateEngine;

    @Autowired
    public void setTemplateEngine(TemplateEngine templateEngine) {
        this.templateEngine = templateEngine;
    }


    @Async("asyncServiceExecutor")
    @Override
    public void sendEmailVerifyMessage(String subject, String content, String template, String verificationCode, User user) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            var helper = new MimeMessageHelper(mimeMessage, true);
            // 由谁发送
            helper.setFrom(from);
            // 发送给谁
            helper.setTo(user.email());
            // 标题
            helper.setSubject(subject);
            var context = new Context();
            context.setVariable("user", user);
            context.setVariable("verificationCode", verificationCode);
            String mail = templateEngine.process(template, context);
            helper.setText(content, mail);
            // 开启线程发送邮件
            javaMailSender.send(mimeMessage);
            log.info("邮件发送成功! 用户：" + user);
        } catch (Exception e) {
            log.error("邮件发送失败: " + e.getMessage());
        }
    }

    // private void sendMessage(String to, String subject, String content, String verificationCode, String template) {
    //     MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    //     try {
    //         var helper = new MimeMessageHelper(mimeMessage, true);
    //         helper.setFrom(from);
    //         helper.setTo(to);
    //         helper.setSubject(subject);
    //         Context context = new Context();
    //         // TODO 根据用户名称去找用户
    //         User user = new User(1, "ITGuoKe", "123456", "男", "2915154639@qq.com", "18536475134", ".");
    //         context.setVariable("user", user);
    //         context.setVariable("code", verificationCode);
    //         // String mail = templateEngine.process("/mail/emailVerify", context);
    //         String mail = templateEngine.process(template, context);
    //         helper.setText(content, mail);
    //         // 发送
    //         javaMailSender.send(mimeMessage);
    //         log.info("邮件发送成功! ");
    //     } catch (Exception e) {
    //         log.error("邮件发送失败: " + e.getMessage());
    //     }
    // }
}
